package ParcelDelivery;

import java.util.Properties;

import org.apache.log4j.Logger;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.InvalidName;
import org.omg.CosNaming.NamingContextPackage.NotFound;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManager;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import ParcelDelivery.DeliveryMgr.DeliveryMgr;
import Util.Util;
import be.fundp.info.util.logging.MyLogger;

/**
 * Parcel delivery main
 * @author Loup
 *
 */
public class DeliveryMain {
	public static final String file = "Delivery.properties";
	private static String ip;
	private static String port;
	public static Logger logger;
	private static final String log4j = "log4j.properties";
	
	public static void main(String args[]) throws NotFound, CannotProceed, InvalidName {

		if(!MyLogger.isConfigured())
			MyLogger.initLogger(log4j);
		logger = MyLogger.getLogger(Class.class);
		
		ip = Util.getPropertiesAttribute(file, "ip");
		port = Util.getPropertiesAttribute(file, "port");
		
		if(ip == null || port == null){
			logger.info("[ERROR] file "+file+ " is absent");
			return;
		}
	



		// build properties for orb
		Properties props = new Properties();
		props.put("org.omg.CORBA.ORBInitialHost", ip);
		props.put("org.omg.CORBA.ORBInitialPort", port);

		// initialize ORB and rootPOA
		ORB orb = ORB.init(new String[] { "-ORBInitialPort", port, "-ORBInitiaHost", ip }, props);
		POA rootPOA = null;

		try {
			rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
		} catch (org.omg.CORBA.ORBPackage.InvalidName e) {
			logger.fatal("unable to resolve rootpoa, exiting.\n", e);
			System.exit(1);
		}

		// get naming service reference (using RootPOA)
		NamingContext naming = NamingContextHelper.narrow(orb.string_to_object("corbaloc:iiop:" + ip + ":" + port + "/NameService"));

		NameComponent[] name = new NameComponent[] { new NameComponent("Delivery", "Object") };
		// create servant
		DeliveryMgr deliveryMgr_impl = new DeliveryMgr();
		try {
			rootPOA.activate_object(deliveryMgr_impl);
		} catch (Exception e) {
			logger.error(e);
		}

		// bind object into the naming service
		try {
			naming.bind(name, deliveryMgr_impl._this_object());
		} catch (Exception e) {
			naming.rebind(name, deliveryMgr_impl._this_object());
			logger.fatal(e);
		} 
		logger.debug("published " + orb.object_to_string(deliveryMgr_impl._this_object()));
		// get manager and activate it
		POAManager manager = rootPOA.the_POAManager();
		try {
			manager.activate();
		} catch (AdapterInactive e) {
			logger.fatal("cannot activate manager.\n", e);
			System.exit(1);
		}
		// block thread until orb is stopped
		logger.info("run orb, hang current thread.");
		orb.run();
	}

}
